<?
function get_the_comments($post_id = null, $comment_parent = 0, $level = 0, $sort_order = "DESC") {
	global $drizzo_users, $drizzo_comments;
	
	use_db_drizzo();
	
	$comment_query = mysql_query("SELECT * FROM `$drizzo_comments` WHERE `post_id`='$post_id' AND `status`='approved' AND `parent_id`='$comment_parent' ORDER BY `ID` $sort_order");
	
	if($comment_query) {

		while($row = mysql_fetch_array($comment_query)) {
			$comment_id = $row['ID'];
			$title = html_entity_decode(stripslashes($row['title']));
			$content = html_entity_decode(stripslashes($row['content']));
			$author_id = $row['author_id'];
			$author_name = $row['author_name'];
			$author_url = $row['author_url'];
			$author_email = $row['author_email'];
			$date = $row['date'];
			$parent_id = $row['parent_id'];
			
			if($author_id != 0) {
				$author_lookup = mysql_query("SELECT * FROM `$drizzo_users` WHERE `ID`='$author_id' LIMIT 1");
				if($author_lookup) {
					$author_array = mysql_fetch_array($author_lookup);
					$author_name = $author_array['nice_name'];
					if($author_name == null) {
						$author_name = $author_array['username'];
					}
					$author_url = $author_array['url'];
					$author_email = $author_array['email'];
				}
			}
			
			if($parent_id == $comment_parent) {
				$the_comments_array[] = array(
					"title" => $title,
					"content" => $content,
					"author_id" => $author_id,
					"author" => $author_name,
					"author_url" => $author_url,
					"author_email" => $author_email,
					"date" => $date,
					"level" => $level,
					"parent_id" => $parent_id
					);
					
					get_the_comments($post_id, $comment_id, $level +1);
			}
		}

//		echo "<pre>";
//		var_dump($the_comments_array);
//		echo "</pre>";

		return $the_comments_array;

	} else {
		return false;
	}
}

function the_comments($post_id = null, $style = "ol", $ajax = false) {
	global $the_post;
	
	if($the_post) {
		$post_id = $the_post['ID'];
	}
	$no = 0;
	$comments = get_the_comments($post_id);

	if($comments) {
		if($style == "ul") {
			$comments_string .= "<ul id=\"comments_list\">\n";
		}

		if($style == "ol") {
			$comments_string .= "<ol id=\"comments_list\">\n";
		}

		foreach($comments as $comment) {
			$level = $comment['level'];
			if($the_post['author'] == $comment['author_id']) {
				$comments_string .= "\t<li class=\"comment author\" style=\"padding-left: " . $level . "00%;\">";
			} else {
				$comments_string .= "\t<li class=\"comment\" style=\"padding-left: " . $level . "00%;\">";
			}
			$gravatar_url = "http://www.gravatar.com/avatar.php?gravatar_id=" . $comment['author_email'];
			$user_avatar_url = "http://" . domain_name . admin_folder . "engine/user_avatar.php?user_id=" . $comment['author_id'];
			$comments_string .= "<span class=\"avatar\">";
			if($comment['author_url']) {
				$comments_string .= "<a href=\"" . $comment['author_url'] . "\">";
			}
			if(file_exists($gravatar_url)) {
				$comments_string .= "<img src=\"" . $gravatar_url . "\" alt=\"Gravatar\" class=\"avatar_img\" /><br />";
			} else {
				$comments_string .= "<img src=\"" . $user_avatar_url . "\" alt=\"Avatar\" class=\"avatar_img\" /><br />";
//			} else {
				// Do Nothing
			}
			$comments_string .= $comment['author'];
			if($comment['author_url']) {
				$comments_string .= "</a>";
			}
			$comments_string .= "</span>";
			if($comment['title'] != null) {
				$comments_string .= "<h3 class=\"comment_title\">" . $comment['title'] . "</h3>\n";
			}
			$comments_string .= "<span class=\"comment_date\">Posted on " . date("M d, Y \at g:i A", $comment['date']) . "</span>";
			$comments_string .= "<div class=\"comment_content\">" . $comment['content'] . "</div>";
			$comments_string .= "<br class=\"clear\" /></li>\n";
		}

		if($style == "ul") {
			$comments_string .= "</ul>\n";
		}

		if($style == "ol") {
			$comments_string .= "</ol>\n";
		}
		
		if($ajax) {
			$comments_string .= "<h2><a href=\"javascript:void(0)\" onclick=\"new Ajax.Updater('comment_box', '" . the_theme('directory') . "comment_box.php', {onComplete:function(){ new Effect.Appear('comment_box');},asynchronous:true, evalScripts:true})\" title=\"Add a Comment\">Add a Comment</a></h2>\n";
			$comments_string .= "<div id=\"comment_box\" style=\"display: none;\"></div>\n";
		} else {
			$comments_string .= "<h2>Add a Comment</h2>\n";
			$comments_string .= "<div id=\"comment_box\">";
			$comment_box_uri = the_theme('directory') . "comment_box.php";
			$comments_string .= file_get_contents($comment_box_uri);
			$comments_string .= "</div>\n";
		}
		
		return $comments_string;
	} else {
		return false;
	}
}
?>